package com.workplan.client.widgets.addshifts;

import java.util.ArrayList;
import java.util.Date;

import com.general.client.action.ActionHandler;
import com.general.client.basewidget.BaseWidget;
import com.general.client.position.Position;
import com.general.client.widgets.button.green.GreenButton;
import com.general.client.widgets.checkbox.Checkbox;
import com.general.client.widgets.combobox.Combobox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.workplan.shared.datamodel.workweek.Workweek.WorkDays;

public class AddShifts extends BaseWidget implements AddShiftsPresenter.Display {

	@UiField
	Label label;

	@UiField
	Combobox<WorkDays> dayCombo;
	@UiField
	Combobox<Integer> fromHour;
	@UiField
	Combobox<Integer> fromMin;
	@UiField
	Combobox<Integer> toHour;
	@UiField
	Combobox<Integer> toMin;
	@UiField
	Combobox<Integer> empNum;

	@UiField
	Checkbox sunday;
	@UiField
	Checkbox monday;
	@UiField
	Checkbox tuesday;
	@UiField
	Checkbox wednesday;
	@UiField
	Checkbox thursday;
	@UiField
	Checkbox friday;
	@UiField
	Checkbox saturday;

	@UiField
	GreenButton addButton;

	private static AddShiftsUiBinder uiBinder = GWT
			.create(AddShiftsUiBinder.class);

	interface AddShiftsUiBinder extends UiBinder<Widget, AddShifts> {
	}

	public AddShifts() {
		initWidget(uiBinder.createAndBindUi(this));

		addButton.getText().setText("Add");
		label.setText("Add Shifts");

		dayCombo.setLabel("Day");
		dayCombo.setLabelPosition(Position.ABOVE);
		for (WorkDays wd : WorkDays.values()) {
			dayCombo.addItem(wd, wd.name());
		}

		configureCombobox(fromHour, "From", 24, 1);
		configureCombobox(fromMin, "", 60, 15);
		configureCombobox(toHour, "To", 24, 1);
		configureCombobox(toMin, "", 60, 15);
		configureCombobox(empNum, "# Emp.", 5, 1);

		initWorkday(sunday, WorkDays.SUNDAY, "Sun.");
		initWorkday(monday, WorkDays.MONDAY, "Mon.");
		initWorkday(tuesday, WorkDays.TUESDAY, "Tue.");
		initWorkday(wednesday, WorkDays.WEDNESDAY, "Wed.");
		initWorkday(thursday, WorkDays.THURSDAY, "Thu.");
		initWorkday(friday, WorkDays.FRIDAY, "Fri.");
		initWorkday(saturday, WorkDays.SATURDAY, "Sat.");

		setDefaultValues();
	}

	@Override
	public void setDefaultValues() {
		dayCombo.setValue(WorkDays.MONDAY);
		fromHour.setValue(9);
		fromMin.setValue(0);
		toHour.setValue(15);
		toMin.setValue(0);
		empNum.setValue(2);

		sunday.setValue(false);
		monday.setValue(false);
		tuesday.setValue(false);
		wednesday.setValue(false);
		thursday.setValue(false);
		friday.setValue(false);
		saturday.setValue(false);
	}

	private void initWorkday(final Checkbox dayCheckbox, WorkDays value,
			String label) {

		dayCheckbox.setLabel(label);
		dayCheckbox.setLabelPosition(Position.ABOVE);
		dayCheckbox.setValue(false);
	}

	private void configureCombobox(Combobox<Integer> combo, String title,
			Integer maxValue, int jumps) {
		combo.setLabel(title);
		combo.setLabelPosition(Position.ABOVE);
		for (Integer i = 0; i < maxValue; i += jumps) {
			combo.addItem(i, i.toString());
		}
	}

	@Override
	public HandlerRegistration addAddActionHandler(ActionHandler handler) {
		return addButton.addActionHandler(handler);
	}

	@Override
	public ArrayList<WorkDays> getSelectedDays() {
		ArrayList<WorkDays> result = new ArrayList<WorkDays>();
		WorkDays selectedDay = dayCombo.getValue();
		if (null != selectedDay)
			result.add(selectedDay);
		addConditionalDay(result, sunday, WorkDays.SUNDAY, selectedDay);
		addConditionalDay(result, monday, WorkDays.MONDAY, selectedDay);
		addConditionalDay(result, tuesday, WorkDays.TUESDAY, selectedDay);
		addConditionalDay(result, wednesday, WorkDays.WEDNESDAY, selectedDay);
		addConditionalDay(result, thursday, WorkDays.THURSDAY, selectedDay);
		addConditionalDay(result, friday, WorkDays.FRIDAY, selectedDay);
		addConditionalDay(result, saturday, WorkDays.SATURDAY, selectedDay);
		return result;
	}

	private void addConditionalDay(ArrayList<WorkDays> result,
			Checkbox dayCheckbox, WorkDays dayValue, WorkDays selectedDay) {
		if (dayCheckbox.getValue() && selectedDay != dayValue)
			result.add(dayValue);
	}

	@Override
	public Date getStartTime() {
		return extrapolateTime(fromHour, fromMin);
	}

	@Override
	public Date getEndTime() {
		return extrapolateTime(toHour, toMin);
	}

	@SuppressWarnings("deprecation")
	private Date extrapolateTime(Combobox<Integer> hour, Combobox<Integer> min) {
		Date result = new Date();
		result.setHours(hour.getValue());
		result.setMinutes(min.getValue());
		return result;
	}

	@Override
	public int getEmployeesCount() {
		return empNum.getValue();
	}
}
